package com.rainmonth.nicehair.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.Bind;

import com.rainmonth.nicehair.R;
import com.rainmonth.nicehair.ui.activity.HairCommentActivity;
import com.rainmonth.nicehair.common.CircleImageView;
import com.rainmonth.nicehair.common.model.Comment;
import com.rainmonth.nicehair.utils.ImageFetcher;

public class HairCommentAdapter extends BaseAdapter {

    private Context mContext;
    public List<Comment> mListInfo;
    private ImageFetcher mImageFetcher;

    public HairCommentAdapter(Context context, List<Comment> listInfo, ImageFetcher imageFetcher) {
        this.mContext = context;
        this.mListInfo = listInfo;
        this.mImageFetcher = imageFetcher;
    }

    @Override
    public int getCount() {
        return mListInfo == null ? 1 : mListInfo.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void addItemLast(List<Comment> datas) {
        mListInfo.addAll(datas);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.hair_scan_comment_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final Comment com = mListInfo.get(position);
        mImageFetcher.loadImage(com.getHeadUrl(), holder.mHeadIv);
        holder.mContentTv.setText(com.getContent());
        holder.mTimeTv.setText(com.getTime());
        if (!TextUtils.isEmpty(com.getPicUrl())) {
            holder.mLayout.setVisibility(View.VISIBLE);
            holder.mScanIv.setVisibility(View.VISIBLE);
            mImageFetcher.loadImage(com.getPicUrl(), holder.mScanIv);
            holder.mRightTv.setText(String.valueOf(position));
            holder.mNoRightTv.setText("0");
        } else {
            holder.mLayout.setVisibility(View.GONE);
            holder.mScanIv.setVisibility(View.GONE);
        }
        holder.mSendIv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HairCommentActivity.HAIR_COMMENT_ACTION);
                intent.putExtra("uid", com.getUid());
                intent.putExtra("name", com.getName());
                mContext.sendBroadcast(intent);
            }
        });
        return convertView;
    }

    class ViewHolder {
        @Bind(R.id.scan_item_head)
        CircleImageView mHeadIv;
        @Bind(R.id.scan_comment_content)
        TextView mContentTv;
        @Bind(R.id.scan_item_img)
        ImageView mScanIv;
        @Bind(R.id.scan_comment_time)
        TextView mTimeTv;
        @Bind(R.id.scan_comment_send)
        ImageView mSendIv;
        @Bind(R.id.replay_layout)
        LinearLayout mLayout;
        @Bind(R.id.right_num_tv)
        TextView mRightTv;
        @Bind(R.id.no_right_num_tv)
        TextView mNoRightTv;
        @Bind(R.id.reply_comment_layout)
        LinearLayout mLayoutMore;
        @Bind(R.id.reply_more_tv)
        TextView mMoreTv;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
